const { ccclass, property } = cc._decorator;

import n from "./EventMgr"
import l from "./MatchGameEventType"
import s from "./frameworkManager"
import c from "./MatchGameData"
import u from "./MatchGameConfig"
import p, { ObstacleAllType, ObstacleAllType1 } from "./ObstacleConfig"
import d from "./MatchGameAudioMgr"
import h from "./MatchGameDataMgr"
import f, { SquareStateMachineState } from "./SquareStatusMgr"
import g from "./ObstacleCtrl"
import y from "./SquareItem"
import m from "./MatchGameUtilsDataMgr"
import _ from "./obstaclePrefab"
import v from "./ObstacleAniCtrl"
import b from "./TargetAniCtrl"
import C from "./HoneyPrefab"
import T from "./MatchGameCenterCtrl"
import M from "./MatchGameViewCtrl"
import SquareItem from "./SquareItem";
import MatchGameConfig from "./MatchGameConfig";

@ccclass
export default class ObstaclePrefabCtrl extends SquareItem {

    public layout = 1;
    public _startPos = null;
    public _moveOff = false;
    public itemInfo = null;
    public emptyObstacleArr = [];
    public isClearAll = false;
    public magicHatTouchCount = -1;
    public willEliminated = false;
    public ui;

    public onLoad() {
        this.onUILoad();
        this.addButtonListen();
    };
    public start() { };
    public onEnable() {
        this.addEvent();
    };
    public onDisable() {
        this.removeEvent();
    };
    public onUILoad() {
        this.ui = this.node.addComponent(_);
    };
    public addButtonListen() { };
    public initData() { };
    public addEvent() {
        n.listen(l.BIRD_FINISH_FALL_DOWN, this.onBirdFinishFallDownEvent, this);
    };
    public removeEvent() {
        n.ignore(l.BIRD_FINISH_FALL_DOWN, this.onBirdFinishFallDownEvent, this);
    };
    public init(t, a, o) {
        this.ui.itemParent.removeAllChildren();
        this.squareType = u.SquareType.ObstacleItem;
        this.itemInfo = o;
        this.canMove = this.itemInfo.MoveStatus;
        this._couldTouch = this.itemInfo.MoveStatus;
        this.willEliminated = false;
        if (this.itemInfo.name == ObstacleAllType1.MHT) {
            this.magicHatTouchCount = 3;
            if (this.ui.spine) {
                this.ui.spine.getComponent(sp.Skeleton).skeletonData = null;
                v.playMagicAni(this.ui.spine, 0, false);
            }
        } else if (this.itemInfo.name == ObstacleAllType1.MAI) v.playMailBoxAni(this.ui.spine, this.itemInfo); else if (this.itemInfo.name == ObstacleAllType1.BID) {
            this.ui.spine.getComponent(sp.Skeleton).skeletonData = null;
            v.loadBirdIdleAni(this.ui.spine);
        } else v.playObstacleAni(this.itemInfo, this.ui.spine);
        super.init(t, a, u.SquareType.ObstacleItem, this.itemInfo.ObstacleAllType, this.itemInfo);
        this.layerType = this.itemInfo.LayoutType;
        this.ui.IconNode.active = false;
        this.ui.spine.active = true;
        if (this._couldTouch) {
            this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
            this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
            this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
        }
    };
    public setHoney(e) {
        if (this.itemInfo.name == ObstacleAllType1.HOY) {
            this.ui.IconNode.active = false;
            this.ui.spine.active = false;
            if (this.ui.itemParent.getChildByName("HoneyPrefab")) this.ui.itemParent.getChildByName("HoneyPrefab").getComponent(C).init(e.direArr, e.angleArr); else {
                var t = cc.instantiate(T._instance.honeyPrefab);
                this.ui.itemParent.addChild(t);
                t.getComponent(C).init(e.direArr, e.angleArr);
            }
        }
    };
    public initEditorHoney() {
        var e = this;
        s.log("initEditorHoney");
        this.ui.IconNode.active = true;
        this.ui.spine.active = false;
        m.loadImage("imgs/game/obstacle/" + this.getPathStr(), this.ui.IconNode.getComponent(cc.Sprite), function () {
            if (e.ui) {
                e.ui.IconNode.width = 78;
                e.ui.IconNode.height = 80;
            }
        }, function () {
            s.log("加载图片报错---");
        });
    };
    public getPathStr() {
        var e;
        if (this.itemInfo.name == ObstacleAllType1.WBX || this.itemInfo.name == ObstacleAllType1.GRA || this.itemInfo.name == ObstacleAllType1.VAS || this.itemInfo.name == ObstacleAllType1.HOY || this.itemInfo.name == ObstacleAllType1.OYS || this.itemInfo.name == ObstacleAllType1.FLP || this.itemInfo.name == ObstacleAllType1.PFL || this.itemInfo.name == ObstacleAllType1.STO || this.itemInfo.name == ObstacleAllType1.PPG || this.itemInfo.name == ObstacleAllType1.EGG || this.itemInfo.name == ObstacleAllType1.GCN || this.itemInfo.name == ObstacleAllType1.RCK) e = this.itemInfo.name + "_" + this.itemInfo.Hp; else if (this.itemInfo.name == ObstacleAllType1.CBX) {
            this.itemInfo.SpecifyColor <= 0 && s.log("颜色木盒配表报错了！！！");
            e = this.itemInfo.name + "_" + this.itemInfo.SpecifyColor + "_" + this.itemInfo.Hp;
        } else this.itemInfo.name == ObstacleAllType1.MHT ? e = this.itemInfo.name + "_open" : this.itemInfo.name == ObstacleAllType1.BID ? e = this.itemInfo.name + "_1" : this.itemInfo.name == ObstacleAllType1.MAI && (e = this.itemInfo.name);
        return e;
    };
    public setMagicClose() {
        if (0 != this.itemInfo.enable) {
            d.playMailExitAudio();
            v.playMagicAni(this.ui.spine, 0, true);
            this.itemInfo.enable = false;
        }
    };
    public setMailBoxClear() {
        if (0 != this.itemInfo.enable) {
            v.playMailBoxAni(this.ui.spine, this.itemInfo);
            this.itemInfo.enable = false;
        }
    };
    public setClose() { };
    public setIconNode() {
        var e = this.ui.IconNode;
        e.x = e.width / 2;
        e.y = -e.height / 2;
    };

    public _couldTouch = false;
    public touchStartEvent(t?) {
        super.touchStartEvent.call(this, t);
        if (this._couldTouch) {
            this._startPos = t.getLocation();
            this._moveOff = true;
        }
    };
    public touchMoveEvent(e) {
        if (this._couldTouch && this._moveOff && f.getSquareNormalStatus(this)) {
            var t = e.getLocation();
            if (this._startPos) {
                var a = Math.abs(t.x - this._startPos.x), o = Math.abs(t.y - this._startPos.y);
                a > o && a > MatchGameConfig.ITEM_WIDTH / 2 && t.x - this._startPos.x < 0 ? this.setOtherChoose({
                    row: this._row - 1,
                    col: this._col
                }) : o > a && o > MatchGameConfig.ITEM_HEIGHT / 2 && t.y - this._startPos.y > 0 ? this.setOtherChoose({
                    row: this._row,
                    col: this._col - 1
                }) : a > o && a > MatchGameConfig.ITEM_WIDTH / 2 && t.x - this._startPos.x > 0 ? this.setOtherChoose({
                    row: this._row + 1,
                    col: this._col
                }) : o > a && o > MatchGameConfig.ITEM_HEIGHT / 2 && t.y - this._startPos.y < 0 && this.setOtherChoose({
                    row: this._row,
                    col: this._col + 1
                });
            }
        }
    };
    public touchEndEvent() {
        s.log("touchEnd", this);
        this._moveOff = true;
    };
    public setChoose() { };
    public setOtherChoose(e) {
        if (this._couldTouch && this._moveOff && T._instance.displayMapInfo[e.row] && T._instance.displayMapInfo[e.row][e.col]) if (T._instance.displayMapInfo[e.row][e.col] != u.DisplayMapType.Normal || h.middleSquareGrid[e.row][e.col]) if (h.middleSquareGrid[e.row] && h.middleSquareGrid[e.row][e.col] && f.getSquareNormalStatus(this) && h.middleSquareGrid[e.row][e.col].canMove && f.getSquareNormalStatus(h.middleSquareGrid[e.row][e.col])) {
            
            this._moveOff = false;
            h.changeObstacleAndOther({
                i: this._row,
                j: this._col
            }, {
                i: e.row,
                j: e.col
            });
        } else {
            this._moveOff = false;
            this.touchWall(e);
        } else {
            this._moveOff = false;
            h.changeSquareAndEmpty({
                i: this._row,
                j: this._col
            }, {
                i: e.row,
                j: e.col
            });
        }
    };
    public touchWall(e) {
        var t = this, a = this.node.getChildByName("spine"), o = 20 * -(this._row - e.row), i = 20 * (this._col - e.col), r = cc.v3(a.x, a.y), n = cc.v3(a.x + o, a.y + i);
        s.log("touchWall", o, i);
        // this.node.zIndex = 999;
        cc.tween(a).to(.05, {
            position: n
        }).to(.05, {
            position: r
        }).call(function () {
            a.position = r;
            t.node.zIndex = 0;
        }).start();
        cc.tween(a).to(.06, {
            scaleX: 0 !== o ? 1 : 1.2,
            scaleY: 0 !== i ? 1 : 1.2
        }).to(.04, {
            scaleX: 1,
            scaleY: 1
        }).call(function () {
            a.scaleX = 1;
            a.scaleY = 1;
        }).start();
    };
    public clear() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
    };
    public moveTo(t, a, o) {
        super.moveTo.call(this, t, a, o);
    };
    public destorySelf() {
        super.destorySelf.call(this);
    };
    public onForceClear() {
        var e = this;
        if (!this.squareFsm.is(SquareStateMachineState.changing) && !this.isClearAll) {
            this.isClearAll = true;
            var t = null;
            if (this.itemInfo.Hp < 0) if (this.itemInfo.name == ObstacleAllType1.MHT) {
                s.log("魔术帽消除", this.itemInfo, this.magicHatTouchCount);
                if (!this.itemInfo.enable) return;
                this.magicHatTouchCount = 1;
                if (1 == this.magicHatTouchCount) {
                    v.playMagicAni(this.ui.spine, this.magicHatTouchCount, false);
                    d.playMagicStateAudio(2);
                }
            } else this.itemInfo.name == ObstacleAllType1.MAI && 1 == this.itemInfo.enable && g.createMailItem(this); else {
                this.itemInfo.Hp = 0;
                this.itemInfo.name == ObstacleAllType1.GCN && 1 == this.itemInfo.enable && g.createGoldCoinFlyItem(this);
                this.itemInfo.name == ObstacleAllType1.FLP && g.createFlower(this._row, this._col);
                if (this.itemInfo.name == ObstacleAllType1.RCK) {
                    d.playRockBreakAudio(2);
                    g.createDiamondItem(this);
                }
                this.itemInfo.Hp = 0;
                let t = () => {
                    s.log("destory------");
                    T._instance.recoverSquare(e);
                };
            }
            this.playAni();
            t && t();
        }
    };
    public onClear() {
        var e = this;
        let t1;
        if (!f.getSquareChangeStatus(this) && !this.isClearAll) {
            var t = null;
            if (this.itemInfo.Hp < 0) {
                if (this.itemInfo.name == ObstacleAllType1.MHT) {
                    s.log("魔术帽消除", this.itemInfo, this.magicHatTouchCount);
                    if (!this.itemInfo.enable) return;
                    this.magicHatTouchCount -= 1;
                    if (1 == this.magicHatTouchCount) {
                        v.playMagicAni(this.ui.spine, this.magicHatTouchCount, false);
                        d.playMagicStateAudio(2);
                    } else if (2 == this.magicHatTouchCount) {
                        v.playMagicAni(this.ui.spine, this.magicHatTouchCount, false);
                        d.playMagicStateAudio(1);
                    } else this.magicHatTouchCount;
                    if (this.magicHatTouchCount <= 0) {
                        v.playMagicAni(this.ui.spine, this.magicHatTouchCount, false);
                        this.magicHatTouchCount = 3;
                        g.createGemMatchItem(this._row, this._col);
                    }
                } else if (this.itemInfo.name == ObstacleAllType1.MAI && 1 == this.itemInfo.enable) {
                    s.log("邮箱消除");
                    g.createMailItem(this);
                }
            } else if (this.itemInfo.Hp > 1) {
                this.itemInfo.Hp -= 1;
                f.normalSquare(this);
                if (this.itemInfo.name == ObstacleAllType1.RCK) {
                    d.playRockBreakAudio(1);
                    this.itemInfo.aroundMatch = true;
                }
            } else if (this.itemInfo.Hp <= 1) {
                this.itemInfo.name == ObstacleAllType1.GCN && 1 == this.itemInfo.enable && g.createGoldCoinFlyItem(this);
                this.itemInfo.name == ObstacleAllType1.FLP && g.createFlower(this._row, this._col);
                if (this.itemInfo.name == ObstacleAllType1.RCK) {
                    d.playRockBreakAudio(2);
                    g.createDiamondItem(this);
                }
                this.itemInfo.Hp = 0;
                t1 = () => {
                    s.log("destory------");
                    T._instance.recoverSquare(e);
                };
            }
            this.playAni();
            t1 && t1();
        }
    };
    public playAni() {
        this.itemInfo.name !== ObstacleAllType1.MHT && this.itemInfo.name !== ObstacleAllType1.BID && v.playObstacleAni(this.itemInfo, this.ui.spine);
        (f.getSquareNormalStatus(this) || f.getSquareReadyDestoryStatus(this)) && v.playObstacleEffectAni({
            row: this._row,
            col: this._col
        }, this.itemInfo);
        this.itemInfo.name == ObstacleAllType1.MAI && 1 == this.itemInfo.enable && v.playMailBoxAni(this.ui.spine, this.itemInfo);
    };
    public checkBirdFly() {
        this.squareType == u.SquareType.ObstacleItem && this.itemType == ObstacleAllType.Bird && this.onBirdFinishFallDown(this.node);
    };
    public playBirdFallFinishAni() {
        s.log("playBirdFallAni--");
        this.squareType == u.SquareType.ObstacleItem && this.itemType == ObstacleAllType.Bird && v.playBirdIdleAni(this.ui.spine, false);
    };
    public onBirdFinishFallDownEvent(e) {
        if (e && this._row == e.row && this.squareType == u.SquareType.ObstacleItem && this.itemType == ObstacleAllType.Bird) {
            var t = T._instance.dropInfo;
            if (t[this._row] && t[this._row].endIndexY <= this._col) {
                var a = c.isTargetType(u.SquareType.ObstacleItem, this.itemType);
                if (a) {
                    s.log("小鸟---");
                    v.playBirdDownAni(this.ui.spine, true);
                    d.playBirdCollectAudio();
                    b.playBirdFlyAni(this.node.position, M.instance.targetItems[a.index], function () {
                        n.trigger(l.FLY_ONE_TARGET, {
                            index: a.index
                        });
                    });
                }
                T._instance.recoverSquare(this);
            } else {
                s.log("小鸟下落完成2");
                v.playBirdDownAni(this.ui.spine, true);
            }
        }
        s.log("小鸟下落完成3");
        this.checkBirdIsDown();
    };
    public checkBirdIsDown() {
        if (this.squareType == u.SquareType.ObstacleItem && this.itemType == ObstacleAllType.Bird) {
            var e = T._instance.dropInfo;
            if (e[this._row] && e[this._row].endIndexY <= this._col) {
                var t = c.isTargetType(u.SquareType.ObstacleItem, this.itemType);
                if (t) {
                    s.log("小鸟---");
                    v.playBirdDownAni(this.ui.spine, true);
                    d.playBirdCollectAudio();
                    b.playBirdFlyAni(this.node.position, M.instance.targetItems[t.index], function () {
                        n.trigger(l.FLY_ONE_TARGET, {
                            index: t.index
                        });
                    });
                }
                T._instance.recoverSquare(this);
            }
        }
    };
    public onBirdFinishFallDown(e) {
        var t = T._instance.dropInfo;
        if (t[this._row] && t[this._row].endIndexY <= this._col) {
            var a = c.isTargetType(u.SquareType.ObstacleItem, this.itemType);
            if (a) {
                s.log("小鸟---");
                v.playBirdDownAni(this.ui.spine, true);
                d.playBirdCollectAudio();
                b.playBirdFlyAni(e.position, M.instance.targetItems[a.index], function () {
                    n.trigger(l.FLY_ONE_TARGET, {
                        index: a.index
                    });
                });
            }
            T._instance.recoverSquare(this);
        } else {
            s.log("小鸟下落完成2");
            v.playBirdDownAni(this.ui.spine, true);
        }
    };

    public getTargetLeftCount() {
        return this.itemInfo.name == ObstacleAllType1.MAI ? 0 : 1;
    };
    public getElimilateItem() {
        var e = h.middleSquareGrid;
        if (this.node && this.itemInfo.enable) {
            if (this.itemInfo.ObstacleAllType == ObstacleAllType.Bird) {
                for (var t = [], a = this._row, o = this._col + 1; o < u.MAX_COL; o++) if (e[a] && e[a][o] && !f.getSquareDestoryStatus(e[a][o]) && e[a][o].itemType !== ObstacleAllType.Bird) {
                    var i = e[a][o];
                    if (i.squareItemInfo && 0 == i.squareItemInfo.enable) continue;
                    t.push(e[a][o]);
                }
                return t.length ? t : null;
            }
            return [this];
        }
        return null;
    };
}
